#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""量子金融分析系统启动器
允许用户选择登录方式：传统登录或ATM风格登录
"""

import os
import sys
import time
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, 
                            QLabel, QPushButton, QMessageBox, QGroupBox)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont

# 添加项目根目录到系统路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from utils.logger import Logger

class LoginMethodSelector(QWidget):
    """登录方式选择器"""
    def __init__(self):
        super().__init__()
        self.setWindowTitle("量子金融分析系统 - 登录方式选择")
        self.setMinimumSize(500, 300)
        self.init_ui()
    
    def init_ui(self):
        """初始化UI界面"""
        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(40, 40, 40, 40)
        main_layout.setSpacing(30)
        
        # 标题
        title_label = QLabel("欢迎使用量子金融分析系统")
        font = QFont()
        font.setBold(True)
        font.setPointSize(18)
        title_label.setFont(font)
        title_label.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(title_label)
        
        # 描述
        desc_label = QLabel("请选择登录方式：")
        desc_label.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(desc_label)
        
        # 按钮组
        buttons_group = QGroupBox()
        buttons_layout = QHBoxLayout(buttons_group)
        buttons_layout.setSpacing(20)
        
        # 传统登录按钮
        self.traditional_login_btn = QPushButton("传统登录")
        self.traditional_login_btn.setFixedHeight(50)
        self.traditional_login_btn.setStyleSheet("font-size: 16px;")
        self.traditional_login_btn.clicked.connect(self.launch_traditional_mode)
        
        # ATM风格登录按钮
        self.atm_login_btn = QPushButton("账户快捷登录")
        self.atm_login_btn.setFixedHeight(50)
        self.atm_login_btn.setStyleSheet("font-size: 16px;")
        self.atm_login_btn.clicked.connect(self.launch_atm_mode)
        
        # 添加按钮到布局
        buttons_layout.addWidget(self.traditional_login_btn)
        buttons_layout.addWidget(self.atm_login_btn)
        
        main_layout.addWidget(buttons_group)
        
        # 提示信息
        tip_label = QLabel("提示：账户快捷登录模式像银行柜员机一样，输入证券账户或卡号后直接进入交易所主页面")
        tip_label.setAlignment(Qt.AlignCenter)
        tip_label.setWordWrap(True)
        main_layout.addWidget(tip_label)
        
        # 设置窗口居中显示
        self.center()
    
    def center(self):
        """将窗口居中显示"""
        qr = self.frameGeometry()
        cp = QApplication.desktop().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
    
    def launch_traditional_mode(self):
        """启动传统登录模式"""
        Logger.log_info("用户选择传统登录模式")
        try:
            # 导入并运行主程序
            from main_window import main as traditional_main
            # 关闭当前窗口
            self.close()
            # 启动传统模式
            traditional_main()
        except Exception as e:
            Logger.log_error(f"启动传统登录模式失败: {str(e)}")
            QMessageBox.critical(self, "启动失败", f"无法启动传统登录模式: {str(e)}")
    
    def launch_atm_mode(self):
        """启动ATM风格登录模式"""
        Logger.log_info("用户选择ATM风格登录模式")
        try:
            # 导入并运行ATM风格登录程序
            from atm_style_login import main as atm_main
            # 关闭当前窗口
            self.close()
            # 启动ATM模式
            atm_main()
        except Exception as e:
            Logger.log_error(f"启动ATM风格登录模式失败: {str(e)}")
            QMessageBox.critical(self, "启动失败", f"无法启动账户快捷登录模式: {str(e)}")

def main():
    """启动器主入口"""
    # 初始化日志
    Logger.init_logger()
    Logger.log_info("量子金融分析系统启动器已启动")
    
    # 创建应用程序实例
    app = QApplication(sys.argv)
    
    # 创建登录方式选择器
    selector = LoginMethodSelector()
    selector.show()
    
    # 运行应用程序
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()